"""
认证接口定义
===========
定义认证和授权的抽象接口，支持JWT、OAuth、LDAP等多种认证方式
"""

from abc import ABC, abstractmethod
from typing import Any, Optional, Dict, List
from datetime import datetime, timedelta


class TokenInterface(ABC):
    """令牌接口"""
    
    @abstractmethod
    def get_user_id(self) -> str:
        """获取用户ID"""
        pass
    
    @abstractmethod
    def get_username(self) -> str:
        """获取用户名"""
        pass
    
    @abstractmethod
    def get_roles(self) -> List[str]:
        """获取用户角色"""
        pass
    
    @abstractmethod
    def get_permissions(self) -> List[str]:
        """获取用户权限"""
        pass
    
    @abstractmethod
    def get_claims(self) -> Dict[str, Any]:
        """获取所有声明"""
        pass
    
    @abstractmethod
    def is_expired(self) -> bool:
        """检查是否过期"""
        pass
    
    @abstractmethod
    def get_expiry(self) -> datetime:
        """获取过期时间"""
        pass


class AuthInterface(ABC):
    """认证接口定义"""
    
    @abstractmethod
    async def authenticate(self, username: str, password: str) -> Optional[TokenInterface]:
        """用户认证"""
        pass
    
    @abstractmethod
    async def generate_token(self, user_id: str, username: str, roles: List[str] = None, 
                           expire_in: Optional[timedelta] = None) -> str:
        """生成访问令牌"""
        pass
    
    @abstractmethod
    async def verify_token(self, token: str) -> Optional[TokenInterface]:
        """验证令牌"""
        pass
    
    @abstractmethod
    async def refresh_token(self, refresh_token: str) -> Optional[str]:
        """刷新令牌"""
        pass
    
    @abstractmethod
    async def revoke_token(self, token: str) -> bool:
        """撤销令牌"""
        pass
    
    @abstractmethod
    async def has_permission(self, token: TokenInterface, permission: str) -> bool:
        """检查权限"""
        pass
    
    @abstractmethod
    async def has_role(self, token: TokenInterface, role: str) -> bool:
        """检查角色"""
        pass
    
    @abstractmethod
    async def get_user_info(self, token: TokenInterface) -> Dict[str, Any]:
        """获取用户信息"""
        pass
    
    @abstractmethod
    async def change_password(self, user_id: str, old_password: str, new_password: str) -> bool:
        """修改密码"""
        pass
    
    @abstractmethod
    async def reset_password(self, user_id: str, new_password: str) -> bool:
        """重置密码"""
        pass


class PasswordHasherInterface(ABC):
    """密码哈希接口"""
    
    @abstractmethod
    def hash_password(self, password: str) -> str:
        """哈希密码"""
        pass
    
    @abstractmethod
    def verify_password(self, password: str, hashed: str) -> bool:
        """验证密码"""
        pass
